using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnShow_Click(object sender, EventArgs e)
    {
        CrystalReportViewer1.ReportSource = getReportDocument();
        CrystalReportViewer1.DataBind();
    }


    // Returns a ReportDocument object with the report and data loaded 
    private ReportDocument getReportDocument() 
    {
        
        // File Path for Crystal Report
        string repFilePath = Server.MapPath("CrystalReport.rpt");

        // Declare a new Crystal Report Document object
        // and the report file into the report document
        ReportDocument repDoc = new ReportDocument();
        repDoc.Load(repFilePath);

        // Set the datasource by getting the dataset from business layer
        // In our case business layer is getCustomerData function
        repDoc.SetDataSource(getCustomerData());

        return repDoc;

    }

    private DataSet getCustomerData()
    {
        string customerFilePath = Server.MapPath("App_Data\\Customers.xml");
        DataSet ds = new DataSet();
        ds.ReadXml(customerFilePath);
        return ds;
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        // Get the report document
        ReportDocument repDoc = getReportDocument();
        // Stop buffering the response
        Response.Buffer = false;
        // Clear the response content and headers
        Response.ClearContent();
        Response.ClearHeaders();
        try
        {
            // Export the Report to Response stream in PDF format and file name Customers
            repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Customers");
            // There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            ex = null;
        }
    }

}
